package com.kai.controller;

import com.kai.pojo.Coach;
import com.kai.pojo.User;
import com.kai.pojo.dto.CoachDto;
import com.kai.service.AddressService;
import com.kai.service.CoachService;
import com.kai.service.CourseService;
import com.kai.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 教练
 */
@RestController
public class CoachController {
    @Autowired
    private CoachService coachService;
    /**
     * 修改教练信息或添加教练
     * @param coach
     * @return
     */
    @RequestMapping("/addCoach")
    public String addCoach(@RequestBody CoachDto coach){
        System.out.println(coach);
        if (!coach.getId().equals("")){
            coachService.updateCoach(coach,coach.getProvinceId(),coach.getCityId(),coach.getCountyId());
            return "updateCoach";
        }else {
            if (coachService.selectCoachByName(coach.getName())>0){
                return "exist";
            }
            if (Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(coach.getPhone()).matches()){
                coach.setDatecreated(new Date());
                coach.setId(UUIDUtils.getUUID());
                coachService.addCoach(coach,coach.getProvinceId(),coach.getCityId(),coach.getCountyId());
                return "addCoach";
            }
            return "errorPhone";
        }
    }

    /**
     * 查询全部教练或模糊查寝
     * @return
     */
    @RequestMapping("/selectAllCoach")
    public Map<String, Object> selectAllCoach(String name, String sex, int pageNum, int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        int pageTotal = coachService.selectAllCoachCount(name,sex);
        List<Coach> coachList = coachService.selectAllCoach(name,sex,(pageNum-1)*pageSize,pageSize);
        map.put("coachList",coachList);
        map.put("pageTotal",pageTotal);
        return map;
    }
    /**
     * 修改教练状态
     * @param state
     * @param id
     * @return
     */
    @RequestMapping("/updateCoachState")
    @ResponseBody
    public String updateCoachState(String state,String id){
        return coachService.updateCoachState(state, id)+"";
    }
    /**
     *删除教练
     * @param ids
     * @return
     */
    @RequestMapping("/deleteCoach")
    @ResponseBody
    public int deleteUser(String[] ids){
        int i = 0;
        List<String> idList = Stream.of(ids).distinct().collect(Collectors.toList());
        for (String id : idList) {
            i = coachService.deleteCoach(id);
            i++;
        }
        return i;
    }

    /*查询教练*/
    @RequestMapping("/selectCoach")
    public List<Coach> selectCoach(){
        return coachService.selectCoach();
    }
}
